package cn.icanci.ladybird.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * @author: icanci
 * @date: Created in 2020/9/27 20:11
 * @classAction: 配置接口 RestFul 风格的 API 文档生成
 * 接口地址：http://localhost:8380/doc.html
 * 接口访问账户密码：admin 123456
 */
@SuppressWarnings("all")
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class Knife4jConfig {
    @Bean(value = "knife4j")
    public Docket knife4j(Environment environment) {
        //设置需要显示的Knife4j环境
        Profiles profiles = Profiles.of("dev");
        boolean flag = environment.acceptsProfiles(profiles);
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag)
                //分组名称
                .groupName("1.0 版本")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("cn.icanci.ladybird.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    @Bean
    public ApiInfo apiInfo() {
        Contact contact = new Contact("icanci", "https://ladybird.icanci.cn/", "icanci@foxmail.com");
        return new ApiInfo(
                "Ladybird-boot-Web-API",
                "Just Do It",
                "v1.0",
                "https://ladybird.icanci.cn/",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<VendorExtension>()
        );
    }
}
